import Vue from 'vue'
import App from './App'
import { getCode,sendCode } from '@/service/app.js';
import { sessionKey } from '@/utils/config.js';
Vue.config.productionTip = false
// #ifdef MP
Vue.prototype.token = callback=> { // 获取token 微信下
	return getCode(res => {
		let e = false
		sendCode(res.code).then(res1 => {
			if(res1.code == 200){uni.setStorageSync(sessionKey, res1.data.token);e=true}
			callback(e)
		}).catch(err => {callback(e)})
	}, err => {callback(e)})
};
// #endif
// #ifdef H5
Vue.prototype.h5Pay = (data,cb)=>{ // H5支付
   function onBridgeReady() {
	   WeixinJSBridge.invoke('getBrandWCPayRequest',data,
	   function(res) {
		   cb(res)
	   })
   }
   if(typeof WeixinJSBridge == "undefined") {
	   if ( document.addEventListener ) {
		   document.addEventListener('WeixinJSBridgeReady',  onBridgeReady,  false)
	   } else  if  (document.attachEvent) {
		   document.attachEvent('WeixinJSBridgeReady',  onBridgeReady);
		   document.attachEvent('onWeixinJSBridgeReady',  onBridgeReady)
	   }
   } else {
	   onBridgeReady()
   }
}
// #endif
Vue.prototype.setToken = data=> { // 设置token
	return uni.setStorageSync(sessionKey, data)
};
Vue.prototype.getToken = (state=false)=> { // 获取token
	return uni.getStorageSync(sessionKey)?(state?true:uni.setStorageSync(sessionKey)):false
};
Vue.prototype.checkBrowsers = ()=> { // 判断登录浏览器
	let state = true;
	//#ifdef H5
	 // h5页面加判断
		var ua = navigator.userAgent.toLowerCase();
		var isWeixin = ua.indexOf('micromessenger') != -1;
		if (!isWeixin) {
			document.head.innerHTML = '<title>抱歉，出错了</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0"><link rel="stylesheet" type="text/css" href="https://res.wx.qq.com/open/libs/weui/0.4.1/weui.css">';
			document.body.innerHTML ='<div class="weui_msg"><div class="weui_icon_area"><i class="weui_icon_info weui_icon_msg"></i></div><div class="weui_text_area"><h4 class="weui_msg_title">请在微信客户端打开链接</h4></div></div>';
			state = false
		}
	//#endif
	return state
};
App.mpType = 'app'

const app = new Vue({
	...App,
	render: h => h(App)
}).$mount()
export default app;